home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / bipl.zip / PROGS.ZIP / LINDEN.ICN < prev    next >
Text File  |  1992-12-29  |  3KB  |  108 lines

  1. ############################################################################
  2. #
  3. #    File:     linden.icn
  4. #
  5. #    Subject:  Program to generate sentences in 0L-systems
  6. #
  7. #    Author:   Ralph E. Griswold
  8. #
  9. #    Date:     October 11, 1988
  10. #
  11. ###########################################################################
  12. #
  13. #  This program reads in a 0L-system (Lindenmayer system) consisting of
  14. #  rewriting rules in which a string is rewritten with every character
  15. #  replaced simultaneously (conpectually) by a specified string of
  16. #  symbols.
  17. #
  18. #  The last line of input consists of an initial string followed by a colon
  19. #  (which cannot be a symbol in the initial string) and the number of times
  20. #  the rewriting rules are to be applied.  An example is
  21. #
  22. #    1->2#3
  23. #    2->2
  24. #    3->2#4
  25. #    4->504
  26. #    5->6
  27. #    6->7
  28. #    7->8(1)
  29. #    8->8
  30. #    (->(
  31. #    )->)
  32. #    #->#
  33. #    0->0
  34. #    1:14
  35. #
  36. #  Here, the initial string is "1" and the rewriting rules are to be
  37. #  applied 14 times.
  38. #
  39. #  If no rule is provided for a character, the character is not changed
  40. #  by rewriting. Thus, the example above can be expressed more concisely
  41. #  as
  42. #
  43. #    1->2#3
  44. #    3->2#4
  45. #    4->504
  46. #    5->6
  47. #    6->7
  48. #    7->8(1)
  49. #    1:14
  50. #
  51. #  If -a is given on the command line, each rewriting is written out.
  52. #  Otherwise, only the final result is written out.
  53. #
  54. #  Reference:
  55. #
  56. #     Formal Languages, Arto Salomaa, Academic Press, 1973. pp. 234-252.
  57. #
  58. ############################################################################
  59. #
  60. #  Links: options
  61. #
  62. ############################################################################
  63.  
  64. link options
  65.  
  66. global rewrite
  67.  
  68. procedure main(args)
  69.    local line, count, axiom, detail, opts, i, result, s, c, symbol
  70.  
  71.    rewrite := table()
  72.  
  73. #  What follows is a trick.  It takes advantage of the fact that Icon
  74. #  functions are first-class data objects and that function invocation
  75. #  and mutual evaluation have the same syntax.  If -a is specified,
  76. #  the value of "detail" becomes the function for writing and the
  77. #  value of "write" becomes 1.  See below.
  78.  
  79.    detail := 1
  80.  
  81.    opts := options(args,"a")
  82.    if \opts["a"] then detail :=: write
  83.  
  84.    while line := read() do
  85.       line ? {
  86.          if symbol := move(1) & ="->" then 
  87.             rewrite[symbol] := tab(0)
  88.          else if result := tab(upto(':')) then {
  89.             move(1)
  90.             count := tab(0)
  91.             }
  92.          else write(&errout, "malformed input: ", tab(0))
  93.          }
  94.  
  95.    detail(result)            # initial configuration
  96.  
  97.    every 1 to count do {
  98.       s := ""
  99.       every c := !result do
  100.          s ||:= (\rewrite[c] | c)
  101.       result := s
  102.       detail(result)
  103.       }
  104.  
  105.    write(result)            # write result if not already written
  106.  
  107. end
  108.